/*
 * Dialog.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.layouts.BorderLayout;

/**
 * Dialog Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Dialog extends Window {

// Public Static Fields. -------------------------------------------------------

  /**
   * Rejected dialog result (the value is {@value}).
   */
  public static final int REJECTED = 0;

  /**
   * Accepted dialog result (the value is {@value}).
   */
  public static final int ACCEPTED = 1;

// Package-Private Fields. -----------------------------------------------------

  boolean modal;
  boolean modalVisible;
  int result;

// Protected Constructors. -----------------------------------------------------

  /**
   * Constructs new Dialog object.
   */
  public Dialog() {
    this(null);
  }

  /**
   * Constructs new Dialog object.
   *
   * @param title the dialog title
   */
  public Dialog(String title) {
    super(new BorderLayout(), title);
  }

  /**
   * Constructs new Dialog object.
   *
   * @param owner the dialog owner
   * @param title the dialog title
   */
  public Dialog(Window owner, String title) {
    super(new BorderLayout(), title);
    if (owner != null) {
      owner.acquireWindow(this);
    }
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Sets whether or not the dialog is modal.
   *
   * @param modal <tt>true</tt> if modal
   */
  public void setModal(boolean modal) {
    if (modal) {
      if (owner == null) {
        setModalityType(APPLICATION_MODAL);
      } else {
        setModalityType(WINDOW_MODAL);
      }
    } else {
      setModalityType(NON_MODAL);
    }
  }

  /**
   * Returns dialog result.
   *
   * @return dialog result
   */
  public int getDialogResult() {
    return result;
  }

  /**
   * Returns <tt>true</tt> if the dialog is accepted.
   *
   * @return <tt>true</tt> if the dialog is accepted
   */
  public boolean isAccepted() {
    return (result == ACCEPTED);
  }

  /**
   * Returns <tt>true</tt> if the dialog is rejected.
   *
   * @return <tt>true</tt> if the dialog is rejected
   */
  public boolean isRejected() {
    return (result == REJECTED);
  }

  /**
   * Accepts/rejects the dialog.
   *
   * @param accepted <tt>true</tt> if accepted; <tt>false</tt> if rejected
   */
  public void setAccepted(boolean accepted) {
    if (accepted) {
      closeDialog(ACCEPTED);
    } else {
      closeDialog(REJECTED);
    }
  }

  /**
   * Shows modal dialog.
   *
   * @return dialog result.
   */
  public int showDialog() {
    if (!modalVisible) {
      disposedCheck();
      modalVisible = true;
      int result = _exec(_delegate);
      modalVisible = false;
      doDefaultCloseOperation();
      return result;
    }
    return REJECTED;
  }

  /**
   * Closes modal dialog.
   *
   * @param result the dialog result
   */
  public void closeDialog(int result) {
    if (modalVisible) {
      disposedCheck();
      _done(_delegate, result);
    }
  }

// Package-Private Native Method. ----------------------------------------------

  native long _create();

  native int _exec(long delegate);

  native void _done(long delegate, int result);

}
